package cn.kuang.service;

import cn.kuang.mapper.CategoryMapper;
import cn.kuang.pojo.Category;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author 徽州大都督
 * @date 2020/7/27
 */
@Service
@Transactional
public class CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    public List<Category> findAll() {

        if (redisTemplate.opsForValue ().get ("categoryList") != null) {

            //System.out.println ("缓存查询");

            return (List<Category>) redisTemplate.opsForValue ().get ("categoryList");

        } else {
            List<Category> categoryList = categoryMapper.selectList (null);

            //存十分钟
            redisTemplate.opsForValue ().set ("categoryList",categoryList,10, TimeUnit.MINUTES);

           // System.out.println ("数据库查询");
            return categoryList;
        }


    }
}
